<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor :itemList="[
        { name: 'box-base', title: '基础信息' },
        { name: 'box-spec', title: '规格及单位' },
        { name: 'box-zc', title: '注册证信息' },
         { name: 'box-his', title: '医保信息' },
         { name: 'box-edit', title: '最近修改' }]" />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="cpPropRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-base" class="box-title">基础信息
            <span class="span-tip" >修改完将更新已供货商品信息</span>
            <span class="span-tip" v-if="cpIsHospitalMaterial">供货商品请到院内系统修改推送</span>
          </div>
          <customFormItem label="商品名称" name="materialName" :parent="_self" :form="form" ref="materialName" width2x/>
<!--          <customFormItem label="品牌名称" name="materialTradeName" :parent="_self" :form="form" />-->
          <customFormItem label="商品类型" name="materialTypeCode" :parent="_self" :form="form" :type="constCommon.formItemType.selectDict.name" :config="{dictType: constCommon.dictType.baseMatrType}"/>
          <customFormItem
            label="平台品类"
            name="catalogName"
            :parent="_self"
            :form="form"
            :readonly="true"
            :showSelectIcon="true"
            @focus="$refs.goodsCategoryDrawer.show()"
          />
          <customFormItem
            label="供应商"
            name="supplierName"
            :parent="_self"
            :form="form"
            :readonly="true"
            :showSelectIcon="true"
            @focus="$refs.tableDrawerSp.show()"
            :disabled="spDisabled"
            help="保存完不可修改"
            v-if="!utils.isSp()"
          />

          <customFormItem
            label="生产厂商" name="manufacturerName" :parent="_self" :form="form"
            @focusTo="$refs.manufacturerName.focus()"
            :type="constCommon.formItemType.slot.name"
            width2x
            help-bottom="进口耗材，厂家可以写实际厂家也可以写进口代理人名称"
          >
            <template #content>
              <mfgSelect
                v-model="form.manufacturerId"
                :valueDisplay="form.manufacturerName"
                @change="tableDrawerMfgConfirm"
                ref="manufacturerName"
              />
              <customButton
                title="+"
                help="列表不存在厂商时，可以新增厂商"
                theme="primary"
                size="small"
                @click="utilSelect.openSelectCreateFormMfg(tableDrawerMfgConfirm)"
                v-show="!propReadonly"
                style="font-size: 20px; width: 20px;position: absolute;right: -22px;"
              >
              </customButton>
            </template>
          </customFormItem>

          <customFormItem label="助记码" name="pinyin" :parent="_self" :form="form" disabled/>
          <customFormItem label="批准文号" name="approvalNo" :parent="_self" :form="form" />
          <customFormItem label="禁用" name="flagDisabledScp" :parent="_self" :form="form"
                          :type="constCommon.formItemType.switch.name"
                          :config="{flagValueBoolean: false}"
                          disabled
           help-tip="禁用后，该商品不可供货，也不需要维护资质"
          />
          <customFormItem label="备注" name="remark" :parent="_self" :form="form"
                          width2x
          />

          <div id="box-spec" class="box-title">规格及单位</div>
          <template>
            <customForm :data="form.materialSpec" :rules="cpSpecPropRules" ref="editFormSpec" :disabled="propReadonly">
              <template #content>
                <customFormItem label="规格" name="materialSpec" :parent="_self" :form="form.materialSpec"   ref="materialSpec"
                                help-bottom="可以指定为规格或者规格+型号"
                />

                <customFormItem label="寄售" name="isConsignment" :parent="_self" :form="form.materialSpec"
                                :type="constCommon.formItemType.switch.name"
                                :config="{flagValueBoolean: false}"
                                help-bottom="寄售：用后结算，非寄售：入库结算"
                />
                <customFormItem label="标签管理" name="isOneThingOneYard" :parent="_self" :form="form.materialSpec"
                                :type="constCommon.formItemType.switch.name"
                                :config="{flagValueBoolean: false}"
                                help-bottom="需要通过HIS进行扫码收费的商品"
                />
<!--                <customFormItem label="编码格式" name="inputCodingType" :parent="_self" :form="form.materialSpec"
                                :type="constCommon.formItemType.selectDict.name"
                                :config="{dictType: constCommon.dictType.baseInputCodingType}"
                                ref="inputCodingType"
                />-->

                <customFormItem label="单价" name="price" :parent="_self" :form="form.materialSpec"  :type="constCommon.formItemType.inputNumber.name"
                                ref="price"
                                help-bottom="最小单品的价格"
                />
                <customFormItem
                  label="单品单位" name="unit" :parent="_self" :form="form.materialSpec"
                  @focusTo="$refs.unit.focus()"
                  :type="constCommon.formItemType.slot.name"
                  help-bottom="最小单位"
                  ref="unit"
                >
                  <template #content>
                    <unitSelect
                      v-model="form.materialSpec.unit"
                      :valueDisplay="form.materialSpec.unit"
                      @change="unitSelect"
                      ref="unit"
                    />
                  </template>
                </customFormItem>


                <customFormItem
                  label="订单单位" name="orderUnit" :parent="_self" :form="form.materialSpec"
                  @focusTo="$refs.orderUnit.focus()"
                  :type="constCommon.formItemType.slot.name"
                  help-bottom="采购订单使用的单位"
                  ref="orderUnit"
                >
                  <template #content>
                    <unitSelect
                      v-model="form.materialSpec.orderUnit"
                      :valueDisplay="form.materialSpec.orderUnit"
                      @change="orderUnitSelect"
                      ref="orderUnit"
                    />
                  </template>
                </customFormItem>

                <customFormItem label="订单单位比率" name="orderQty" :parent="_self" :form="form.materialSpec"   :type="constCommon.formItemType.inputNumber.name"
                                help-bottom="即可以换算成多少个基本单位数量"
                                ref="orderQty"
                />
              </template>
            </customForm>
          </template>

          <div id="box-zc" class="box-title">注册证信息</div>
          <customFormItem label="注册证号" name="certificateNo" :parent="_self" :form="form" width2x
                          help-bottom="根据实际情况填写，没有的话填无"
          />
          <customFormItem label="注册证开始" name="certificateExpiredStartDate" :parent="_self" :form="form"
                          :type="constCommon.formItemType.datePicker.name" />
          <customFormItem label="注册证截止" name="certificateExpiredDate" :parent="_self" :form="form"
                          :type="constCommon.formItemType.datePicker.name"
                          help-bottom="长期注册证，注册证止可以设置为2099-01-01"
          />

          <div id="box-his" class="box-title">医保信息</div>
          <customFormItem label="医保代码27位" name="hisMedicareNo2" :parent="_self" :form="form" width2x help-bottom="没有的话请填无"/>
          <customFormItem label="医保名称27位" name="hisMedicareName2" :parent="_self" :form="form" width2x help-bottom="没有的话请填无"/>
          <customFormItem label="医保代码20位" name="hisMedicareNo" :parent="_self" :form="form" width2x/>
          <customFormItem label="医保名称20位" name="hisMedicareName" :parent="_self" :form="form" width2x/>

          <div id="box-edit" class="box-title">最近修改</div>
          <customFormItem label="创建时间" name="gmtCreate" :parent="_self" :form="form" disabled/>
          <customFormItem label="修改时间" name="gmtModified" :parent="_self" :form="form" disabled/>
        </template>
      </customForm>
    </div>


    <!--供应商选择-->
    <tableDrawer ref="tableDrawerSp" refName="tableDrawerSp" @confirm="tableDrawerSpConfirm" viewKey="suppliers_crud" attach="body" loadForce/>

    <!--商品分类选择-->
    <goodsCategoryDrawer ref="goodsCategoryDrawer" :form="form"/>
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'
import customUpload from '@/components/custom/form/customUpload.vue'
import goodsCategoryDrawer from '@/components/custom/bus/goodsCategoryDrawer.vue'
import mfgSelect from '@/components/custom/bus/select/mfgSelect.vue'
import unitSelect from '@/components/custom/bus/select/unitSelect.vue'

export default {
  help: '医院管理-详情',
  data() {
    return {
      form: {
        materialSpec: {

        }
      },
      viewData: {},
      spDisabled: true,
      specFormRequiredList: ['materialSpec','price','unit','orderUnit','orderQty']
    }
  },
  methods: {
    // 当前登录供应商信息
    getCurrentSp() {
      let deptType = this.utils.getCurrentUser().deptType
      if (this.constCommon.deptType.supplier != deptType) {
        this.spDisabled = false
        return
      }
      let spInfo = this.utils.getSpInfo()
      if (spInfo && spInfo.id) {
        this.$set(this.form, 'supplierId', spInfo.id)
        this.$set(this.form, 'supplierName', spInfo.name)
        this.spDisabled = true
      }
    },

    // 厂商选择后
    tableDrawerMfgConfirm(row, refName) {
      this.$set(this.form, 'manufacturerId', row.id)
      this.$set(this.form, 'manufacturerName', row.name)
    },

    // 供应商选择后
    tableDrawerSpConfirm(row, refName) {
      this.$set(this.form, 'supplierId', row.id)
      this.$set(this.form, 'supplierName', row.name)
    },

    unitSelect(row) {
      this.$set(this.form.materialSpec, 'unit', row.name)
    },

    orderUnitSelect(row) {
      this.$set(this.form.materialSpec, 'orderUnit', row.name)
    },
  },
  created() {
    this.$emit('event-register', {
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
        // return new Promise(resolve => {
        //   this.$refs.editForm.validate().then((result) => {
        //     if (result && result == true) {
        //       console.log('主表单验证通过')
        //       this.$refs.editFormSpec.validate().then((result) => {
        //         if (result && result == true) {
        //           console.log('规格验证通过')
        //           callback()
        //           resolve()
        //         } else{
        //           console.log('规格验证失败', result)
        //         }
        //       })
        //     } else{
        //       console.log('主表单验证失败', result)
        //     }
        //   })
        // })
      },
      beforeSave: () => {
        let spType = this.form.spType || []
        if (spType.includes(this.constCommon.busConst.matrTypeMatr)){
          this.$set(this.form, 'isConsumable', 1)
        }
        if (spType.includes(this.constCommon.busConst.matrTypeDrug)){
          this.$set(this.form, 'isDrug', 1)
        }
        if (spType.includes(this.constCommon.busConst.matrTypeRreagent)){
          this.$set(this.form, 'isReagent', 1)
        }
        if (this.form.materialSpec.isOneThingOneYard == undefined){
          this.$set(this.form.materialSpec, 'isOneThingOneYard', 0)
        }
        if (this.form.materialSpec.isConsignment == undefined){
          this.$set(this.form.materialSpec, 'isConsignment', 0)
        }
        let isReturn = false
        this.specFormRequiredList.forEach(key => {
          console.log('key', key, this.form.materialSpec[key])
          if (this.strings.isBlank(this.form.materialSpec[key])) {
            this.$refs[key].focus()
            isReturn = true
          }
        })
        if (isReturn)  {
          return  false
        }
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData) => {
        this.form = global.utils.jsonClone(formData)
        if (!this.form.materialSpec) {
          this.form.materialSpec = {}
        }
        this.$set(this.form, 'id', this.form.id)
        if (this.propParent.cpFormNew) {
          this.$set(this.form, 'isDrug', 0)
          this.$set(this.form, 'isConsumable', 0)
          this.$set(this.form, 'isReagent', 0)
          this.getCurrentSp()
          this.$refs.materialName.focus()
        } else {
          let spType = []
          if (this.form.isConsumable == 1) {
            spType.push(this.constCommon.busConst.matrTypeMatr)
          }
          if (this.form.isDrug == 1) {
            spType.push(this.constCommon.busConst.matrTypeDrug)
          }
          if (this.form.isReagent == 1) {
            spType.push(this.constCommon.busConst.matrTypeRreagent)
          }
          this.$set(this.form, 'spType', spType)
          if (this.form.flagDisabledScp) {
            setTimeout(() => {
              this.utilMessage.warning('该商品已禁用，不支持修改')
            }, 1000)
          }
        }
      },
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      },
    })
  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object,
  },
  components: { unitSelect, customAnchor, customUpload, goodsCategoryDrawer, mfgSelect},
  computed: {
    cpPropRules() {
      let rule = this.propRules || {}
      rule.spType = [{ required: true, message: '至少选择一种供货类型', trigger: 'blur' }]
      return rule
    },
    cpSpecPropRules() {
      let rule = {}
      this.specFormRequiredList.forEach(key => {
        rule[key] = [{ required: true, message: '必填', trigger: 'blur' }]
      })
      return rule
    },
    // 是否是医院商品进入
    cpIsHospitalMaterial() {
      return this.propInitData && true == this.propInitData.isHospitalMaterial
    }
  },
}
</script>
